package com.homework.module9part1.controller;

import com.homework.dao.domain.Student;
import com.homework.module9part1.service.StudentService;
import com.homework.util.ResultMsg;
import com.homework.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class StudentController {

    @Autowired
    protected StudentService studentService;

    @RequestMapping("/list")
    @ResponseBody
    public ResultMsg list() {
        List<Student> list = studentService.findAll();

        return ResultUtil.success(list);
    }

    @RequestMapping(value = "/add", method = {RequestMethod.POST})
    @ResponseBody
    public ResultMsg add(@RequestBody Student student) {
        studentService.addStudent(student);
        return ResultUtil.success();
    }

    @RequestMapping("/del/{id}")
    @ResponseBody
    public ResultMsg del(@PathVariable int id) {
        studentService.delStudent(id);

        return ResultUtil.success();
    }

    @RequestMapping(value = "/modify", method = {RequestMethod.POST})
    @ResponseBody
    public ResultMsg modify(@RequestBody Student student) {
        boolean r = studentService.modifyStudent(student);
        if(r == false) {
            return ResultUtil.error(100, "修改失败");
        }

        return ResultUtil.success();
    }
}
